//////////////////////////////////////////////////////////////////////////////////
//
//	Communication Users
//		- Version 1.0
//		- 07.24.2006
//		- diamond-optic
//
//////////////////////////////////////////////////////////////////////////////////
//
// Information:
//
// 	- Can block chat & voice if steamid isnt found in ini file
//
// CVARs: 
//
//	amx_comm_users "1"		//Turn ON(1)/OFF(0)
//	amx_comm_users_say "1"		//Block chat
//	amx_comm_users_say_team "1"	//Block team chat
//	amx_comm_users_voice "1"	//Block voice comm
//
// Known Problems:
//
//	When changing "amx_comm_users_voice" mid-game, clients
//	will need to reconnect for change to take effect...
//
// Changelog:
//
//	- 07.24.2006 Version 1.0
//		Initial Release
//
//////////////////////////////////////////////////////////////////////////////////

#include <amxmodx>
#include <amxmisc>
#include <engine>

#define MAX_USERS 64

new auths[MAX_USERS][35]
new gUserNum = 0
new gChat[33]
new pPlugin, pSay, pSayTeam, pVoice

public plugin_init()
{
	register_plugin("Communication Users","1.0","diamond-optic")
	
	register_cvar("amx_comm_users_stats", "1.0", FCVAR_SERVER|FCVAR_SPONLY)
	
	pPlugin = register_cvar("amx_comm_users","1")
	pSay = register_cvar("amx_comm_users_say","1")
	pSayTeam = register_cvar("amx_comm_users_say_team","1")
	pVoice = register_cvar("amx_comm_users_voice","1")
	
	register_clcmd("say", "funcSay")
	register_clcmd("say_team", "funcSayTeam")
	
	loadConfig()
}

public loadConfig()
{
	new filename[128]
	get_configsdir(filename,127)
	format(filename,127,"%s/comm_users.ini",filename)

	if(!file_exists(filename))
		{
		log_amx("* Error loading ini file: %s",filename)
		return PLUGIN_HANDLED
		}

	new linenum, data[128], txtlen
	while((linenum = read_file(filename,linenum,data,128,txtlen)) != 0)
		{
		if(gUserNum >= MAX_USERS)
			{
			log_amx("* Error - Max chat user limit reached, not all users loaded!!!")
			break
			}
		parse(data,auths[gUserNum],34)

		if(strlen(auths[gUserNum]) > 0)
			gUserNum++
		}
	return PLUGIN_HANDLED
}

public client_putinserver(id)
{
	if(!is_user_connected(id) || get_pcvar_num(pPlugin) != 1)
		return PLUGIN_HANDLED
		
	new steam[35]
	get_user_authid(id,steam, 34)
		
	gChat[id] = 0
	
	if(get_pcvar_num(pVoice) == 1)
		set_speak(id,SPEAK_MUTED)			
	
	for(new i = 0; i < gUserNum; i++)
		{
		if(equal(steam, auths[i]))
			{
			gChat[id] = 1
				
			set_speak(id,SPEAK_NORMAL)
			
			new name[33]
			get_user_name(id, name, 32)
				
			//console_print(id,"communication access granted for: %s (%s)",name,steam)
			log_amx("* Communication Access Granted for: %s (%s)",name,steam)
			}
		}
	return PLUGIN_CONTINUE
}

public funcSay(id)
{
	if(gChat[id] == 0 && get_pcvar_num(pPlugin) == 1 && get_pcvar_num(pSay) == 1)
		return PLUGIN_HANDLED

	return PLUGIN_CONTINUE
}

public funcSayTeam(id)
{
	if(gChat[id] == 0 && get_pcvar_num(pPlugin) == 1 && get_pcvar_num(pSayTeam) == 1)
		return PLUGIN_HANDLED

	return PLUGIN_CONTINUE
}
